<template>
    <div class='family'>
        <el-row :gutter="10" class="mb8" v-if="edit">
            <el-col :span="20">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleShow='add'" :disabled="!customerId">
                    新增家属</el-button>
            </el-col>
            <el-col :span="4">
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-col>
        </el-row>
        <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight>
            <el-table-column label="序号" align="center" prop="id" />
            <el-table-column label="姓名" align="center" prop="name" />
            <el-table-column label="性别" align="center">
                <template slot-scope="scope">
                    <span>
                        {{selectDictLabel(seX, scope.row.sex)}}
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="身份证号" align="center" prop="id_card" />
            <el-table-column label="联系电话" align="center" prop="mobile" />
            <el-table-column label="联系地址" align="center" prop="detailed" />
            <el-table-column label="与老人关系" align="center">
                <template slot-scope="scope">
                    <span>
                        {{selectDictLabel(elderRelative, scope.row.link_type)}}
                    </span>
                </template>
            </el-table-column>
            <el-table-column v-if="edit" label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="form={...form,...JSON.parse(JSON.stringify(scope.row))};handleShow='edit'">
                        编辑
                    </el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">
                        删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="pageCount>0" :pageCount="pageCount" :page.sync="queryParams.page" :limit.sync="queryParams.limit"
            @pagination="getList" />
        <el-dialog :title="handleShow=='add'?'新增':'编辑'" visible v-if="handleShow" @close="close()" width="800px" append-to-body :close-on-click-modal="false">
            <el-row>
                <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-loading="formLoading">
                    <el-col :span="12">
                        <el-form-item label="姓名" prop="name">
                            <el-input style="width:215px;" v-model="form.name" placeholder="请输入姓名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系电话" prop="mobile">
                            <el-input style="width:215px;" v-model="form.mobile" placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="身份证号" prop="id_card">
                            <el-input style="width:215px;" v-model="form.id_card" placeholder="请输入身份证号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="与老人关系:" prop="link_type">
                            <el-select v-model="form.link_type" placeholder="请选择" class="handle-select mr10">
                                <el-option v-for="dict in elderRelative" :key="dict.dictValue" :value="dict.dictValue" :label="dict.dictLabel">
                                    {{ dict.dictLabel }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="性别:" prop="sex">
                            <el-radio-group v-model="form.sex" class="mr10">
                                <el-radio v-for="dict in seX" :key="dict.dictValue" :label="Number(dict.dictValue)">
                                    {{ dict.dictLabel }}
                                </el-radio>z
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系地址" prop="detailed">
                            <el-input style="width:215px;" v-model="form.detailed" placeholder="请输入联系地址" />
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close()">取 消</el-button>
                <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { getCustomerFamilyListsAll, setCustomerFamily, delCustomerFamily } from "@/api/marketing/admit"
import { isvalidatemobile, cardid } from "@/utils/validate";
import { seX, elderRelative } from "@/api/dictionary"
import throttle from 'lodash.throttle'
export default {
    name: 'family',
    props: {
        type: {
            type: String, //add新增edit编辑
            default: ""
        },
        customerId: {
            type: String,
        }
    },
    data() {
        const ismobile = (rule, value, callback) => {
            if (isvalidatemobile(value)[0]) {
                callback(new Error(isvalidatemobile(value)[1]));
            } else {
                callback();
            }
        };
        const cardId = (rule, value, callback) => {
            if (cardid(value)[0]) {
                callback(new Error(cardid(value)[1]));
            } else {
                callback();
            }
        };
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            seX: seX,
            elderRelative: elderRelative,
            queryParams: {
                page: 1,
                limit: 20,
            },
            tableList: [],
            handleShow: "",
            formLoading: false,
            form: {
                id: null,
                customer_id: this.customerId,
                name: null,
                mobile: null,
                id_card: null,
                link_type: null,
                sex: null,
                detailed: null,
            },
            rules: {
                name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                mobile: [
                    { required: true, message: '请输入联系电话', trigger: 'blur' },
                    { validator: ismobile, trigger: "blur" }
                ],
                id_card: [
                    { validator: cardId, trigger: "blur" },
                ],
                link_type: [
                    { required: true, message: "请选择与老人关系", trigger: "blur" },
                ],
            },
            edit: true,
        }
    },
    watch: {
        "customerId"(val) {
            if (val) {
                this.$set(this.form, "customer_id", val)
                this.getList()
            }
        }
    },
    created() {
        this.edit = this.type == "detail" ? false : true
        this.getList()
    },
    methods: {
        getList() {
            this.loading = true;
            if (this.customerId) {
                getCustomerFamilyListsAll({ customer_id: this.customerId }).then(res => {
                    this.tableList = res.data
                    // this.pageCount = res.data.pageCount;
                    this.loading = false;
                });
            }
        },
        close() {
            this.form = {}
            this.$set(this.form, "customer_id", this.customerId)
            this.handleShow = ""
        },
        handleDelete(row) {
            this.$confirm('是否确认删除该家属?删除后将不可恢复!', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delCustomerFamily({ family_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    setCustomerFamily(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.getList();
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),
    }
}
</script>

<style lang='scss' scoped>
/deep/.top-right-btn {
    button:first-child {
        display: none;
    }
}
</style>